//
//  LSNewsViewController.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2018/12/26.
//  Copyright © 2018 wpkj. All rights reserved.
//

#import "LSNewsViewController.h"
#import "LSNewsTableViewSingleImageCell.h"
#import "LSNewsTableViewMultipleImageCell.h"
#import "LSNewsViewModel.h"
#import "LSNewsDetailViewController.h"

@interface LSNewsViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *newsTableView;
@property (nonatomic, strong) NSMutableArray *dataArrayM;
@end

static NSString *LSNewsTableViewSingleImageCellID = @"LSNewsTableViewSingleImageCellID";
static NSString *LSNewsTableViewMultipleImageCellID = @"LSNewsTableViewMultipleImageCellID";

@implementation LSNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"拉萨要闻";
    [self.view addSubview:self.newsTableView];
    self.newsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self loadData];
}

- (void)loadData {
    NSDictionary *param = @{@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token],@"pageNo":@"1",@"count":@"10"};
    [NetworkUtils requestNOHandleWithMethod:@"GET" URL:NewsList param:param completionHandler:^(id responseObject, NSError *connectionError) {
        [self.newsTableView.mj_header endRefreshing];
        if (!connectionError) {
            NSLog(@"返回数据----%@",responseObject);
            NSNumber *status = [responseObject objectForKey:@"status"];
            if (status.integerValue == 200) {
                NSArray *dataArray = [responseObject objectForKey:@"data"];
                if (dataArray > 0) {
                    [self.dataArrayM removeAllObjects];
                    for (NSDictionary *dict in dataArray) {
                        LSNewsViewModel *model = [LSNewsViewModel mj_objectWithKeyValues:dict];
                        if (model) {
                            [self.dataArrayM addObject:model];
                        }
                    }
                    [self.newsTableView reloadData];
                }
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:[NSString stringWithFormat:@"%@",connectionError.localizedDescription]];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArrayM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSNewsViewModel *model = self.dataArrayM[indexPath.row];
//    if ([ValidateUtils validateNull:model.typeImg].length > 1) {
//        LSNewsTableViewMultipleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:LSNewsTableViewMultipleImageCellID forIndexPath:indexPath];
//        cell.model = model;
//        return cell;
//    } else {
        LSNewsTableViewSingleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:LSNewsTableViewSingleImageCellID forIndexPath:indexPath];
        cell.model = model;
        return cell;
//    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    LSNewsViewModel *model = self.dataArrayM[indexPath.row];
    LSNewsDetailViewController *newsDetailVC = [[LSNewsDetailViewController alloc] init];
    newsDetailVC.newsDetailID = model.id;
    [self.navigationController pushViewController:newsDetailVC animated:true];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    LSNewsViewModel *model = self.dataArrayM[indexPath.row];
//    if ([ValidateUtils validateNull:model.typeImg].length > 0) {
//        return (ScreenWidth-LSNewsTableViewMultipleImageCellImageMargin*4)/3+40+30;
//    } else {
//        return 110;
//    }
    LSNewsViewModel *model = self.dataArrayM[indexPath.row];
    CGRect TitleRect = [[ValidateUtils validateNull:model.title] boundingRectWithSize:CGSizeMake(ScreenWidth-30, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceHanSansCN-Regular" size:14]} context:nil];
    return TitleRect.size.height+40;
}

- (NSMutableArray *)dataArrayM {
    if (!_dataArrayM) {
        _dataArrayM = [[NSMutableArray alloc] init];
    }
    return _dataArrayM;
}

- (UITableView *)newsTableView {
    if (!_newsTableView) {
        _newsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
        _newsTableView.backgroundColor = UIColor.whiteColor;
        _newsTableView.delegate = self;
        _newsTableView.dataSource = self;
        [_newsTableView registerClass:LSNewsTableViewSingleImageCell.class forCellReuseIdentifier:LSNewsTableViewSingleImageCellID];
        [_newsTableView registerClass:LSNewsTableViewMultipleImageCell.class forCellReuseIdentifier:LSNewsTableViewMultipleImageCellID];
        _newsTableView.tableFooterView = [UIView new];
    }
    return _newsTableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
